package YuanFuDao2;

import java.util.Arrays;

/**
 * 取出数组中的重复元素
 */
public class Main53 {

    public static void main(String[] args) {
        int[] arr = {1,1,1,2,2,3,5,5,5,5,8};
        System.out.println(Arrays.toString(arr));
        int[] newArr = help(arr);
        System.out.println(Arrays.toString(newArr));
    }

    public static int[] help(int[] arr){
        if(arr.length < 2) return arr;

        int pointer = 0;  // 用pointer记录多少个不同的元素
        for (int i = 1; i < arr.length; i++) {
            if(arr[i] != arr[pointer]){
                pointer++;
                arr[pointer] = arr[i];
            }
        }

        int[] newArr = new int[pointer+1];
        for (int i = 0; i <= pointer ; i++) {
            newArr[i] = arr[i];
        }
        return newArr;
    }


}
